package edu.contoller;

import cn.hutool.core.date.DateTime;
import com.auth0.jwt.interfaces.Claim;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.annotations.LoginRequired;
import edu.entity.Do.BusinessUser;
import edu.entity.Do.Store;
import edu.mapper.BusinessUserMapper;
import edu.mapper.StoreMapper;
import edu.utils.JwtUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/business")
@CrossOrigin(origins = "*",maxAge = 3600)
public class BusinessController {

    @Resource
    BusinessUserMapper businessUserMapper;

    @Resource
    StoreMapper storeMapper;


    @RequestMapping(value = "/doSignUp",method = RequestMethod.POST)
    public String doSignUp(@RequestBody BusinessUser businessUser) {
        Integer phone = businessUserMapper.selectCount(
                new QueryWrapper<BusinessUser>()
                        .eq("phone", businessUser.getPhone())
        );
        Integer account = businessUserMapper.selectCount(
                new QueryWrapper<BusinessUser>()
                        .eq("account", businessUser.getAccount())
        );
        if (phone == 1 || account == 1) {
            return null;
        }

        businessUser.setCreateTime(DateTime.now());
        businessUserMapper.insert(businessUser);

        BusinessUser businessUser1 = businessUserMapper.selectOne(
                new QueryWrapper<BusinessUser>()
                        .eq("phone", businessUser.getPhone())
        );
        Store store = new Store();
        store.setCreateTime(DateTime.now());
        store.setBusinessId(businessUser1.getId());
        storeMapper.insert(store);
        return "success";
    }


    @RequestMapping(value = "/doLogin",method = RequestMethod.GET)
    public String doLogin(String account,String password) {
        BusinessUser businessUser = businessUserMapper.selectOne(
                new QueryWrapper<BusinessUser>()
                        .eq("account", account)
                        .eq("password", password)
        );
        if (businessUser == null) {
            return null;
        }

        Map<String, String> map = new HashMap<>(1);
        map.put("id",String.valueOf(businessUser.getId()));
        return JwtUtil.create(map);
    }


    @LoginRequired
    @RequestMapping(value = "/getBusinessUserInfoById",method = RequestMethod.GET)
    public BusinessUser getBusinessUserInfoById(HttpServletRequest request) {
        String token = request.getHeader("token");
        Map<String, Claim> claims = JwtUtil.getClaims(token);
        Integer id = Integer.parseInt(claims.get("id").asString());
        return businessUserMapper.selectById(id);
    }

    @LoginRequired
    @RequestMapping(value = "/modifyStoreInfo",method = RequestMethod.POST)
    public Integer modifyStoreInfo(@RequestBody Store store) {
        return storeMapper.updateById(store);
    }
}
